<template>
  <div class="banner">
    <div class="item">
      <img :src="dataList[currentIndex] == undefined ? '' : dataList[currentIndex]">
    </div>
    <div class="page" v-if="dataList.length > 1">
      <ul>
        <li @click="gotoPage(prevIndex)">&lt;</li>
        <li v-for="(item,index) in dataList"
            :key="item"
            @click="gotoPage(index)" :class="{'current':currentIndex == index}">  {{index+1}}
        </li>
        <li @click="gotoPage(nextIndex)">&gt;</li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Swipper',
  props: {
    dataList: Array
  },
  data(){
    return{
      currentIndex: 0,   //默认显示图片
      timer: null    //定时器
    }
  },
  methods:{
    //定时器
    runInv(){
      this.timer = setInterval(() => {
        this.gotoPage(this.nextIndex)
      }, 2000)
    },
    gotoPage(index) {
      this.currentIndex = index;
    }
  },
  mounted: function(){
    this.runInv()
  },
  computed:{
    //上一张
    prevIndex() {
      if(this.currentIndex == 0) {
        return this.dataList.length - 1;
      }else{
        return this.currentIndex - 1;
      }
    },
    //下一张
    nextIndex() {
      if(this.currentIndex == this.dataList.length - 1) {
        return 0;
      }else {
        return this.currentIndex + 1;
      }
    }
  }
}
</script>


<style lang="stylus" scoped>
* {
  margin: 0;
  padding: 0;
}
.item img{
	height 375px
}
ul li {
  list-style: none;
  float: left;
  width: 30px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  cursor: pointer;
  color: rgba(255,255,255,.8);
  font-size: 14px;
}
.banner {
  max-width: 1200px;
  margin: 0 auto;
  position: relative;
}
.banner img {
  width: 100%;
  display: block;
}
.banner .page {
  background: rgba(0,0,0,.5);
  position: absolute;
  right: 0;
  bottom: 0;
  width: 100%;
}
.banner .page ul {
  float: right;
}
.current {
  color: #ff6700;
}
</style>